package com.letoken.platform.repo.ecommerce.dao;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.letoken.platform.repo.ecommerce.dto.ShopAfterOrderReq;
import com.letoken.platform.repo.ecommerce.dto.ShopOrderReq;
import com.letoken.platform.repo.ecommerce.dto.UserAfterOrdersReq;
import com.letoken.platform.repo.ecommerce.po.EcommerceAfterOrder;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.letoken.platform.repo.ecommerce.po.EcommerceUserOrder;
import com.letoken.platform.repo.ecommerce.vo.*;
import io.swagger.annotations.ApiModelProperty;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * 售后单 Mapper 接口
 * </p>
 *
 * @author author
 * @since 2023-09-08
 */
public interface EcommerceAfterOrderMapper extends BaseMapper<EcommerceAfterOrder> {


//    @ApiModelProperty(value = "售后状态：0->申请售后；1->申请成功； 2->拒绝")
//    private Integer status;
//
//    @ApiModelProperty(value = "1->退款中 2->已退款 3->拒绝")
//    private Integer refundStatus;
//
//    @ApiModelProperty(value = "1->退货中 2商家同意退 3物流中 4商家收货")
//    private Integer refundGoodsStatus;

    @Select("select ao.* from ecommerce_after_order ao inner join ecommerce_user_order o on  ao.order_id = o.id   where  ao.id = #{id} and o.mall_id = #{mallId}")
    EcommerceAfterOrder modAddressAfter(@Param("id") Integer id , @Param("mallId") Integer mallId);


    // 发货超过七天的订单有没有处理中的售后
    @Select("SELECT  count(1)  from ecommerce_after_order a inner join ecommerce_user_order o on a.order_id = o.id   where o.id  = #{orderId} and a.status <= 1 and a.refund_status != 2 and a.refund_status != 3 and a.refund_goods_status != 4 and a.refund_goods_status != 5 ")
    int existsRunningAfterOrder(@Param("orderId") Integer orderId);




    // 待处理售后
    @Select("SELECT  count(1)  from ecommerce_after_order a inner join ecommerce_user_order o on a.order_id = o.id   where o.mall_id  = #{mallId} and a.status = 0 ")
    int selectAfterOrderByMall1(@Param("mallId") Integer mallId);

    // 处理中
    @Select("SELECT  count(1)  from ecommerce_after_order a inner join ecommerce_user_order o on a.order_id = o.id   where o.mall_id  = #{mallId} and a.status = 1 and a.refund_status != 2 and a.refund_status != 3 and a.refund_goods_status != 4 and a.refund_goods_status != 5 ")
    Integer selectAfterOrderByMall2(@Param("mallId") Integer mallId);

    // 完成
    @Select("SELECT  count(1)  from ecommerce_after_order a inner join ecommerce_user_order o on a.order_id = o.id   where o.mall_id  =  #{mallId} and (a.status = 2  or  a.refund_status = 2  or a.refund_status = 3 or a.refund_goods_status = 4 or a.refund_goods_status = 5)  ")
    Integer selectAfterOrderByMall3(@Param("mallId") Integer mallId);


    @Select("select * from ecommerce_after_order where id = #{id} and user_id = #{userId} and status = #{status}")
    EcommerceAfterOrder selectByIdAndUserIdAndStatus(@Param("id") Integer id , @Param("userId") Integer userId,@Param("status") Integer status);

   // 申请售后的订单  或者 退货退款类型，商家同意售后之后的订单
    @Select("select * from ecommerce_after_order where id = #{id} and user_id = #{userId} and (status = 0  or  (type = 2 and status = 1 and refund_status = 1 and refund_goods_status = 1) )")
    EcommerceAfterOrder selectByIdCancelStatus(@Param("id") Integer id , @Param("userId") Integer userId);


    Page<ShopAfterOrderRes> msgAfterOrderList(@Param("page") Page<ShopAfterOrderRes> page , @Param("req") ShopAfterOrderReq req);


    Page<UserAfterOrderRes> userAfterOrderList(@Param("page") Page<UserAfterOrderRes> page , @Param("req") UserAfterOrdersReq req);





    @Select("select o.id,o.refund_amount, s.user_id as toUserId from ecommerce_user_after_order o inner join ecommerce_user_order p on o.order_id = p.id inner join app_mall_info s on p.mall_id = s.id  where o.id = #{id} and p.user_id = #{userId}")
    UserOrderRefundVo payInfoByOrderId(@Param("userId") Integer userId, @Param("id") Integer id);




    @Select("select a.* from ecommerce_after_order a  inner join ecommerce_user_order o on a.order_id = o.id where a.id = #{id} and o.mall_id = #{mallId} and a.type = #{type}")
    EcommerceAfterOrder getByIdUserIdType(@Param("id")Integer id,@Param("mallId") Integer mallId,@Param("type")Integer type);



    @Select("select 1 from ecommerce_after_order where order_id = #{orderId} and  status  != 3   limit 1")
    Integer isExist(@Param("orderId") Integer orderId);

    @Select("select 1 from ecommerce_after_order where order_id = #{orderId} and status != 3  limit 1")
    Integer isRunningExist(@Param("orderId") Integer orderId);


    @Select("select user_id from app_mall_info where id = #{shopId} ")
    Integer shopUserId(@Param("shopId") Integer shopId);


    @Select("SELECT  count(1)  from ecommerce_after_order a   where  a.user_id = #{userId} and  a.status <= 1 and a.refund_status != 2 and a.refund_status != 3 and a.refund_goods_status != 4 and a.refund_goods_status != 5 ")
    Integer afterNum(@Param("userId") Integer userId);

    Page<StoreAfterSaleOrderRes> afterSaleOrders(@Param("page") Page<ShopAfterOrderRes> page , @Param("req") ShopAfterOrderReq req);

}
